package com.sss.otep.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.KeyStore;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

public class ClientSSLMain {

	private static final String KEYSTORE_FILE = "E:\\client.jks";
	
	private static final String KEYSTORE_PASS = "clientstore";
	private static final String CERTIFACATE_PASS = "clientstore";

	private static final String TRUSTSTORE_FILE = "E:\\truststore.jks";
	private static final String TRUSTSTORE_PASS = "truststore";
	
	private static final String CERTIFACATE_ALIAS = "tomcal-cl";
	private static final String TARGET_URL = "https://localhost:8443/OTEPWebServices/page/";

	private static final String LINE_BREAKER = System.getProperty("line.separator");
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 String targetURL = TARGET_URL;
		   URL url;
		   HttpsURLConnection connection = null;
		   BufferedReader bufferedReader = null;
		   InputStream is = null;

		   try {
		       //Create connection
		       url = new URL(targetURL);
		       //Uncomment this in case server demands some unsafe operations
		       //System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true");
		       connection = (HttpsURLConnection) url.openConnection();

		       connection.setRequestMethod("POST");
		       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		       connection.setRequestProperty("Content-Language", "en-US");

		       SSLSocketFactory sslSocketFactory = getFactory(new File(KEYSTORE_FILE), CERTIFACATE_PASS, CERTIFACATE_ALIAS);
		       connection.setSSLSocketFactory(sslSocketFactory);
		       
		       //Process response
		       is = connection.getInputStream();

		       bufferedReader = new BufferedReader(new InputStreamReader(is));
		       String line;
		       StringBuffer lines = new StringBuffer();
		       while ((line = bufferedReader.readLine()) != null) {
		           lines.append(line).append(LINE_BREAKER);
		       }
		   }catch(Exception e){
			   e.printStackTrace();
		   }

	}

	private static SSLSocketFactory getFactory(File pKeyFile,
			String pKeyPassword, String certAlias) throws Exception {

		/* KEY STORE MANAGER*/
		KeyStore keyStore = KeyStore.getInstance("JKS");							
		keyStore.load(new FileInputStream(pKeyFile), KEYSTORE_PASS.toCharArray());
		
		KeyManagerFactory keyManagerFactory = KeyManagerFactory
		.getInstance("SunX509");
		keyManagerFactory.init(keyStore, CERTIFACATE_PASS.toCharArray());

		/* TRUST STORE MANAGER*/
		KeyStore trustStore = KeyStore.getInstance("JKS");
		trustStore.load(new FileInputStream(new File(TRUSTSTORE_FILE)), TRUSTSTORE_PASS.toCharArray());

		TrustManagerFactory trustManagerFactory =
		    TrustManagerFactory.getInstance("PKIX");
		trustManagerFactory.init(trustStore);
		
		
		
		SSLContext context = SSLContext.getInstance("TLS");
		context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
		return context.getSocketFactory();	
	}
}
